package com.chu.controller;

import com.chu.entity.Employee;
import com.chu.service.EmployeeService;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class LoginController {

    @Autowired
    private EmployeeService employeeService;
//    //根据账号查询用户做登录
//    @RequestMapping("/login")
//    public String login(HttpServletRequest request, String username, String password){
//        Employee employee = employeeService.loginByLoginName(username, password);
//        if(employee!=null){
//            request.getSession().setAttribute("username",username);
//            request.getSession().setAttribute("password",password);
//            request.getSession().setAttribute("employee",employee);
//            return "redirect:index.jsp";
//        }else{
//            return "login";
//        }
//    }
//处理登陆时异常情况的问题
@RequestMapping("/login")
public String login(HttpServletRequest request)throws Exception{
    System.out.println("进入login");
    //得到异常信息 也就是shiro认证失败返回的key值
    String shiroLoginFailure = (String) request.getAttribute("shiroLoginFailure");
    System.out.println(shiroLoginFailure);
    if(shiroLoginFailure!=null){
        //判断是哪个类型的异常

        if(UnknownAccountException.class.getName().equals(shiroLoginFailure)){
            throw new Exception("账号错误");

        }else if(IncorrectCredentialsException.class.getName().equals(shiroLoginFailure)){
            throw new Exception("密码错误");

        }else{
            throw new Exception("未知异常");
        }
    }
    return "login";
}


//    @RequestMapping("/loginout")
//    public String loginOut(HttpServletRequest request){
//        request.getSession().invalidate();
//        return "redirect:login.jsp";
//
//    }


}
